package com.suray.wcs.service.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.commin.core.utils.StringUtils;
import com.suray.wcs.service.enums.CodeLevel;
import com.suray.wcs.service.enums.CodeType;
import com.suray.wcs.service.execption.CustomException;
import com.suray.wcs.service.system.entity.CodeDB;
import com.suray.wcs.service.system.mapper.CodedbMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 操作日志 服务实现类
 * </p>
 *
 * @author cooper
 * @since 2019-06-26
 */
@Service
public class CodeDBServiceImpl extends ServiceImpl<CodedbMapper, CodeDB> {

    /**
     * 根据条件分页查询异常码列表
     *
     * @param codeDB 异常码信息
     * @return 异常码信息集合信息
     */
    public List<CodeDB> selectCodeList(CodeDB codeDB) {
        LambdaQueryWrapper<CodeDB> queryWrapper = new LambdaQueryWrapper();
        if (codeDB.getCodeDec() != null) {
            queryWrapper.eq(CodeDB::getCodeDec, codeDB.getCodeDec());
        }
        if (codeDB.getCodeType() != null) {
            queryWrapper.eq(CodeDB::getCodeType, codeDB.getCodeType());
        }
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 通过错误码查询错误描述
     *
     * @param code
     * @return
     */
    public String getCodeDescByFault(Integer code, CodeType codeType) {
        if (code == null || code == 0) {
            return null;
        }
        QueryWrapper<CodeDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CodeDB::getCodeDec, code).eq(CodeDB::getCodeLevel, CodeLevel.FAULT)
                .eq(CodeDB::getCodeType,codeType);
        CodeDB codedb = this.getOne(queryWrapper, false);
        if (codedb != null) {
            return "错误码:" + code + codedb.getCodeDesc();
        }
        return "错误码:" + code + "无描述";
    }

    /**
     * 根据类型获取异常码
     * @param codeType
     * @return
     */
    public List<CodeDB> getCodeDBsByType(Integer codeType) {
        QueryWrapper<CodeDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CodeDB::getCodeType, codeType);
        List<CodeDB> codedb = this.list(queryWrapper);
        return codedb;
    }

    /**
     * 根据类型获取异常码
     * @param codeType
     * @return
     */
    public String getCodeDescByType(Integer codeType, Integer code) {
        QueryWrapper<CodeDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CodeDB::getCodeType, codeType).eq(CodeDB::getCodeDec, code);
        CodeDB codedb = this.getOne(queryWrapper, false);
        if (codedb != null) {
            return "错误码:" + code + ", " + codedb.getCodeDesc();
        }
        return "错误码:" + code + "无描述";
    }

    /**
     * 通过警告码查询警告描述
     *
     * @param code
     * @return
     */
    public String getCodeDescByWarning(Integer code) {
        if (code == null || code == 0) {
            return null;
        }
        QueryWrapper<CodeDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CodeDB::getCodeDec, code).eq(CodeDB::getCodeLevel, CodeLevel.WARNING);
        CodeDB codedb = this.getOne(queryWrapper, false);
        if (codedb != null) {
            return codedb.getCodeDesc();
        } else {
            return "警告码:" + code + "无描述";
        }
    }

    /**
     * 删除
     *
     * @param codeIds
     * @return
     */
    public int deleteCodeByIds(Integer codeIds) {

        return baseMapper.deleteById(codeIds);
    }

    /**
     * 通过异常码查询
     *
     * @param codeDec
     */
    public CodeDB selectByCodeDec(Integer codeDec) {
        LambdaQueryWrapper<CodeDB> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CodeDB::getCodeDec, codeDec);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 更新异常字典
     *
     * @param codeDB
     */
    public int updateCodeDB(CodeDB codeDB) {
        LambdaUpdateWrapper<CodeDB> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CodeDB::getCodeDec, codeDB.getCodeDec())
                .set(CodeDB::getCodeDesc, codeDB.getCodeDesc())
                .set(CodeDB::getCodeHex, codeDB.getCodeHex())
                .set(CodeDB::getCodeType, codeDB.getCodeType())
                .set(CodeDB::getCodeLevel, codeDB.getCodeLevel())
                .set(CodeDB::getSolution, codeDB.getSolution());

        return baseMapper.update(null, updateWrapper);
    }

    /**
     * 导入异常数据
     *
     * @param codeDBList
     * @param isUpdateSupport
     * @return
     */
    public String importCode(List<CodeDB> codeDBList, boolean isUpdateSupport) {
        if (StringUtils.isNull(codeDBList) || codeDBList.size() == 0) {
            throw new CustomException("导入异常字典数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CodeDB codeDB : codeDBList) {
            try {
                //验证异常码是否存在
                CodeDB code = this.selectByCodeDec(codeDB.getCodeDec());
                if (StringUtils.isNull(code)) {
                    this.save(codeDB);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、异常码 " + codeDB.getCodeDec() + " 导入成功");
                } else if (isUpdateSupport) {
                    this.updateCodeDB(codeDB);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、异常码 " + codeDB.getCodeDec() + " 导入成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、异常码 " + codeDB.getCodeDec() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、异常码 " + codeDB.getCodeDec() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    public boolean checkCodeUnique(CodeDB codeDB) {
        Long codeId = StringUtils.isNull(codeDB.getCodeId()) ? -1L : codeDB.getCodeId();
        CodeDB info = this.selectByCodeDec(codeDB.getCodeDec());
        if (StringUtils.isNotNull(info) && info.getCodeId().longValue() != codeId.longValue()) {
            return true;
        }
        return false;
    }

    public void deleteAllCode() {
        baseMapper.delete(null);
    }
}
